/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Negocio;

/**
 *
 * @author Analia
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class SMTP
{

     private Configuracion configuracion;

     private Socket _socket = null;
     private PrintWriter out = null;
     private BufferedReader in = null;

    public SMTP(Configuracion configuracion)
    {
        this.configuracion = configuracion;
        conexionSMTP();
    }

    public void conexionSMTP()
    {
        try
        {
              _socket = new Socket(configuracion.getDominio(), configuracion.getPuertoSMTP());
              out     = new PrintWriter(_socket.getOutputStream(), true);
              in      = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
        }
        catch (UnknownHostException e)
        {
              System.err.println("ERROR: Host desconocido");
              System.exit(1);
        } 
        catch (IOException e)
        {
              System.err.println("ERROR: No se pudo establecer la conexion al puerto 25 ");
              System.exit(1);
        }
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    }

    private String leerServer(BufferedReader In) throws IOException
    {
        String Datos="", userInput="";
               do
               {
                    userInput = In.readLine();
                    Datos=Datos+"\n"+userInput;
               }
               while(In.ready());

        return Datos;
    }

    public void enviar(String mail_from, String rcpt_to, String subject, String contenido)
    {
        try
        {
            String userInput;
            System.out.println("--SERVIDOR SMTP-- responde: " + leerServer(in));

            userInput="MAIL FROM: " + mail_from;
            System.out.println("--CLIENTE SMTP-- dice: " + userInput);
            out.println(userInput);
            System.out.println("--SERVIDOR SMTP-- responde: " + leerServer(in));


            userInput="RCPT TO: " + rcpt_to;
            System.out.println("CLIENTE SMTP: " + userInput);
            out.println(userInput);
            System.out.println("--SERVIDOR SMTP-- responde: " + leerServer(in));

            userInput="DATA";
            System.out.println("--CLIENTE SMTP-- dice: " + userInput);
            out.println(userInput);
            System.out.println("--SERVIDOR SMTP-- responde: " + leerServer(in));


            userInput="subject: " + subject + "\n" + contenido + "\n.\n";
            System.out.println("--CLIENTE SMTP-- dice: " + userInput);
            out.println(userInput);
            System.out.println("--SERVIDOR SMTP-- responde: " + leerServer(in));
        }
        catch(Exception e)
        {
            System.out.println("ERROR: No se pudo enviar el email ; "+e.getCause());
        }
    }

    public void quit()
    {
        String userInput;
        userInput="QUIT";
        System.out.println("--CLIENTE SMTP-- dice: " + userInput+"\n");
        out.println(userInput);
    }

    public void desconectar()
    {
        out.close();
        try
        {
            in.close();
            _socket.close();
        } 
        catch (IOException e)
        {
     
        }
    }

}
